package com.shawn.controller;

import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;

@Controller
@RequestMapping("/file")
public class FileController {

    private Logger logger = LoggerFactory.getLogger(FileController.class);

    @RequestMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {
        logger.debug("upload(), file_name: " + file);
        if (file == null) {
            logger.warn("upload failed! file is null!");
            return "failed!";
        }
        //获取upload文件夹的路径
        String realPath = request.getRealPath("/upload");
        //将上传的文件写入 upload文件夹 中
        if (file.getOriginalFilename() == null) {
            logger.warn("upload failed! filename is null!");
            return "failed!";
        }
        File target = new File(realPath, file.getOriginalFilename());
        if (!target.exists()) {
            boolean result = target.createNewFile();
            logger.debug("upload(), target create result: " + result);
        }

        file.transferTo(target);
        return "success!";
    }

    @RequestMapping("/download")
    public void download(String fileName, HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取upload文件夹的路径
        String realPath = request.getRealPath("/upload");
        //通过流读取文件
        FileInputStream is = new FileInputStream(new File(realPath, fileName));
        //获得响应流
        ServletOutputStream os = response.getOutputStream();
        //设置响应头信息
        response.setHeader("content-disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "UTF-8"));
        //通过响应流将文件输入流读取到的文件写出
        IOUtils.copy(is, os);
        //关闭流
        IOUtils.closeQuietly(is);
        IOUtils.closeQuietly(os);
    }
}
